package com.lyy.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyy.Vo.QueryKeyWord;
import com.lyy.aspect.ControllerEndpoint;
import com.lyy.response.Result;
import com.lyy.system.entity.LoginLog;
import com.lyy.system.entity.User;
import com.lyy.system.service.LoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/loginlog")
@Api(value = "日志管理",tags = "登录日志接口")
public class LoginLogController {

    @Autowired
    private LoginLogService loginLogService;

    @PostMapping("/findLoginlogList")
    @ApiOperation(value = "登录日志分页查询列表",notes = "条件分页查询")
    public Result findLoginlogList(@RequestParam(required = true,defaultValue = "1") Integer current,
                                   @RequestParam(required = true,defaultValue = "6") Integer size,
                                   @RequestBody QueryKeyWord queryKeyWord){
        //对用户进行分页，泛型中注入的就是实体类
        Page<LoginLog> page =new Page<>(current,size);
        QueryWrapper<LoginLog> wrapper =new QueryWrapper<>();
        if (queryKeyWord!=null){
            if (!StringUtils.isEmpty(queryKeyWord.getKeyWord())){
                wrapper.eq("username",queryKeyWord.getKeyWord());
            }
        }
        IPage<LoginLog> loginLogIPage= loginLogService.findLoginlogList(page,wrapper);
        Long total=loginLogIPage.getTotal();
        List<LoginLog> records=loginLogIPage.getRecords();
        return Result.ok().data("total",total).data("records",records);

    }

    @ControllerEndpoint(exceptionMessage = "删除登录日志失败", operation = "删除登录日志")
    @DeleteMapping("/deleteById")
    @ApiOperation(value = "删除日志",notes = "删除登录日志信息")
    public Result deleteById(Long id){
        loginLogService.deleteById(id);
        return Result.ok();
    }
}
